/*
Plugin: jQuery Ajax Loader Plugin
Version 1.0
*/

(function($){

	$.fn.ajaxLoad = function(url, append, callback){
	
		var $this = $(this);
		var $append = append;
		var $url = url;
				
		$.ajax({
			
			dataType: "html",
			method: "post",
			url: $url,
			statusCode: {
			
				//not found
				404: function() {
					
					if($append)
						$this.append("content not found.");
					else
						$this.html("content not found.");
						
					setTimeout(function(){
					
						callback();
							
					}, animationDuration);
					
				}
				
			}
		
		//on success
		}).success(function(responseText){
		
			if($append)
				$this.append(responseText);
			else
				$this.html(responseText);
			
			if(!animated)
				$(".a").removeClass("a");
			
			setTimeout(function(){
			
				$(".colorbox").colorbox({returnFocus:true, trapFocus:true, overlayClose:true, speed: animationDuration});
				callback();
				
			}, animationDuration);
		 
		//on error
		}).error(function(){
		
			if($append)
				$this.append("error occured!");
			else
				$this.html("error occured!");
				
			setTimeout(function(){
			
				callback();
				
			}, animationDuration);
		  
		});
	
	};

})(jQuery);

/*
Plugin: jQuery Core Slider Plugin
Version 1.0
*/

(function($){

	$.fn.coreSlider = function(delay, animateIn, animateOut){
	
		var $delay = delay;
		var $animateIn = animateIn;
		var $animateOut = animateOut;
		var $this = $(this);
		var $counter = 1;
		var $numOfPosts = $this.find("li").length;
		var $current, $next;
		
		//hide all posts
		$this.find("li").each(function(){
		
			$(this).addClass("hidden");
		
		});
		
		//show and animate the first one
		$this.find("li").eq(0).removeClass("hidden").addClass("animated "+$animateIn);
		
		//add switching interval
		setInterval(function(){
		
			//get the current and next items
			$current = $this.find("li."+$animateIn);
			$next = $this.find("li").eq($counter);
		
			//switch them out
			$current.removeClass("animated "+$animateIn).addClass("animated "+$animateOut);
			setTimeout(function(){
			
				$current.addClass("hidden");
				$next.addClass("animated "+$animateIn).removeClass("hidden "+$animateOut);
				updateHeroContainer();
				
			}, animationDuration*1.5);
			
			$counter++;
			
			//when at the last item, switch to first again
			if($counter >= $numOfPosts)
				$counter = 0;
			
		
		}, delay);
	
	};

})(jQuery);

/*
Plugin: jQuery Scroller Plugin
Version 1.0

*/

(function($){

	$.fn.scroller = function(duration, easing){
	
		var $duration = duration;
		var $easing = easing;
		var $this = $(this);
		var navHeight = $("nav").height();
						
		$this.find("a:not(.ignore)").each(function(){
		
			$(this).on("click", function(evt){
			
				//split the anchor at the #
				var $anchor = $(this).attr("href");
				$anchor = $anchor.split("#");
				$anchor = $("#"+$anchor[1]);
			
				//prevent default scrolling
				evt.preventDefault();
				
				//get the top position of the anchor and animate to it
				var $goTo = Math.round($anchor.offset().top - navHeight);
				
				//animate
				$('html, body').stop().animate({"scrollTop": $goTo +"px"}, {duration:$duration, easeing:$easing});
					
			});
		
		});
	
	};

})(jQuery);

/*
Plugin: Image Preloader Plugin
Version 1.0

*/

(function($){

	$.fn.preloader = function(where, callback){
	
		var $where = where;
		var $nimages = $("img").length;
		var $counter = 1;
		
		//get the background-image attribute of each section and add it as an image to 'where'
		$("section").each(function(){
		
			var $background;
		
			if($(this).css("background-image") != 'none' && $(this).css("background-image") != 'undefined' && $(this).css("background-image") != ''){
				
				$background = $(this).css("background-image");
				$background = $background.replace(/"/g,"").replace(/url\(|\)$/ig, "");
				
				$("#"+$where).append("<img src='"+$background+"' alt='sample' />");
			
			}
		
		});
		
		//close each img element to the 'where' argument provided
		$("img").each(function(){
		
			var $clone = $(this).clone();
			$("#"+$where).append($clone);
			
			$counter++;
			
			//when at last element, callback
			if($counter == $nimages)
				callback();
		
		});
			
	};

})(jQuery);